//
//  WelcomeViewController.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/15.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        super.loadView()
        view = backImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        showAnimation()
    }
    
    //设置界面
    func setupUI() {
        backImageView.addSubview(iconImageView)
        backImageView.addSubview(textLab)
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(backImageView.snp_centerX)
            make.top.equalTo(backImageView.snp_top).offset(screenSize.height * 0.2)
            make.height.equalTo(100)
            make.width.equalTo(100)
        }
        
        textLab.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView.snp_centerX)
            make.top.equalTo(iconImageView.snp_bottom).offset(20)
        }
        
        iconImageView.layer.cornerRadius = 50
        iconImageView.layer.masksToBounds = true
        textLab.alpha = 0
    }
    
    //设置动画
    private func showAnimation() {
        iconImageView.snp_updateConstraints { (make) -> Void in
        make.top.equalTo(backImageView.snp_top).offset(screenSize.height * 0.3)
        }
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
            UIView.animateWithDuration(1, animations: { () -> Void in
                self.textLab.alpha = 1
                }, completion: { (_) -> Void in
                    //发送通知
                    NSNotificationCenter.defaultCenter().postNotificationName(WeiBoSwitchRootController, object: nil)
            })
        }
    }

    //背景图片
    lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //展位图
    lazy var iconImageView: UIImageView = {
        let icon = UIImageView()
        let url = NSURL(string: UserAccountViewModel().iconImage ?? "")
        icon.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        return icon
    }()
    //欢迎Label
    lazy var textLab: UILabel = {
        let lab = UILabel()
        lab.text =  (UserAccountViewModel().userName ?? "") + " 欢迎回来"
        lab.textColor = UIColor.lightGrayColor()
        lab.font = UIFont.systemFontOfSize(20)
        lab.sizeToFit()
        return lab
    }()
    

}
